package com.github.wings27.interview.SortAndSearch;

/**
 * Created with IntelliJ IDEA.
 * Project Name: common-interview-problems
 * Date Created: 2013/11/01 下午8:33
 *
 * @author wings
 */
public class Search {
    public static int binarySearch(int[] array, int value) {
        return binarySearch(array, 0, array.length - 1, value);
    }

    private static int binarySearch(int[] array, int left, int right, int value) {
        if (left >= right) {
            return -1;
        }
        int mid = (left + right) / 2;
        if (value < array[mid]) {
            return binarySearch(array, left, mid, value);
        }
        if (value > array[mid]) {
            return binarySearch(array, mid + 1, right, value);
        }
        if (value == array[mid]) {
            return mid;
        }
        return -1;
    }
}
